from django.shortcuts import render
from rest_framework.filters import OrderingFilter
# Create your views here.

# GET / categories / (?P < category_id >\d +) / skus?page = xxx & page_size = xxx & ordering = xxx
from rest_framework.generics import ListAPIView

from goods.models import SKU
from goods.serializers import SKUIndexSerializer
from users.serializers import SKUSerializer

from drf_haystack.viewsets import HaystackViewSet
class SKUListView(ListAPIView):
    """SKU 列表数据"""
    serializer_class = SKUSerializer
    # 默认的排序方式
    filter_backends = (OrderingFilter,)
    ordering_fields = ('create_time', 'price', 'sales')

    # 里面包含了视图对象
    def get_queryset(self):
        # 取到视图对象里面的路径命名参数
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """SKU搜索视图集"""

    index_models = [SKU]
    serializer_class = SKUIndexSerializer